/* Wasson An
 * The server for the cat and mouse game
 */

package catandmouse;

import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.Timer;


public class CAMServer implements ActionListener{

	private ServerSocket server; //the server
	private Socket sockOne; //the socket for player two
	private Socket sockTwo; //the socket for player one
	private PrintWriter pOne; //the print writer for the first player
	private PrintWriter pTwo; //the print writer for the second player
	private BufferedReader rOne; //the reader for the first player
	private BufferedReader rTwo; //the reader for the second player
	private Boolean oneCat; //holds if player one is the cat
	private Timer timer; //timer to keep the game running


	//0 param constructor
	public CAMServer(){

		server = null;
		sockOne = null;
		sockTwo = null;
		pOne = null;
		pTwo = null;
		rOne = null;
		rTwo = null;
		oneCat = false;
		timer = new Timer(100, this);
		
		timer.setInitialDelay(1000);
		timer.setRepeats(true);
	}//default constructor


	//sets up the server
	private void initializeServer(){

		//creates the server
		try{

			server = new ServerSocket(0);
			System.out.println(server.getLocalPort());
			System.out.println(InetAddress.getLocalHost().getHostAddress());
		}catch(IOException e){

			System.out.println("Something went wrong. @1");
			System.exit(-1);
		}//catch

		//connects with the players and get their name
		try{

			sockOne = server.accept();
			pOne = new PrintWriter(sockOne.getOutputStream());
			rOne = new BufferedReader(new InputStreamReader(
					sockOne.getInputStream()));

			sockTwo = server.accept();
			pTwo = new PrintWriter(sockTwo.getOutputStream());
			rTwo = new BufferedReader(new InputStreamReader(
					sockTwo.getInputStream()));

			if((int)(Math.random() * 2) == 0){

				pOne.println("cat");
				pTwo.println("mouse");
				pOne.flush();
				pTwo.flush();
				oneCat = true;
			}//if

			else{

				pOne.println("mouse");
				pTwo.println("cat");
				pOne.flush();
				pTwo.flush();
				oneCat = false;
			}//else
		}catch(Exception e){

			System.out.println("Something went wrong. @2");
			System.exit(-1);
		}//catch
	}//initializeServer


	//when the timer ticks
	public void actionPerformed(ActionEvent ae){

		System.out.println("Debug: Getting info");
		
		int oX; //player one's x position
		int oY; //player one's y position
		int tX; //player two's x position
		int tY; //player two's y position
		int xDiff; //the x difference
		int yDiff; //the y difference
		
		try{

			System.out.println("Debug: Printing N");
			
			pOne.println('N');
			pTwo.println('N');
			pOne.flush();
			pTwo.flush();

			System.out.println("Debug: Reading positions");
			
			oX = Integer.parseInt(rOne.readLine());
			oY = Integer.parseInt(rOne.readLine());
			tX = Integer.parseInt(rTwo.readLine());
			tY = Integer.parseInt(rTwo.readLine());

			System.out.println("Debug: Printing positions");
			
			pOne.println(tX);
			pOne.println(tY);
			pTwo.println(oX);
			pTwo.println(oY);
			pOne.flush();
			pTwo.flush();
			
			xDiff = oX - tX;
			yDiff = oY - tY;
			
			
			System.out.println("Debug: oX: " + oX);
			System.out.println("Debug: oY: " + oY);
			System.out.println("Debug: tX: " + tX);
			System.out.println("Debug: tY: " + tY);
			System.out.println("Debug: xDiff: " + xDiff);
			System.out.println("Debug: yDiff: " + yDiff);
			
			System.out.println("Debug: Checking Win");
			
			//checks for victory
			if(Math.abs(xDiff) < 10 && Math.abs(yDiff) < 10){

				timer.stop();
				
				if(oneCat){
					
					pOne.write('W');
					pTwo.write('L');
					pOne.flush();
					pTwo.flush();
				}//if
				
				else{
					
					pOne.write('L');
					pTwo.write('W');
					pOne.flush();
					pTwo.flush();
				}//else
				
				System.out.println("Game Over");
				System.exit(0);
			}//if			
		}catch(Exception e){

			System.out.println("Something went wrong running game. @3");
			System.exit(-1);
		}//catch
	}//actionPerformed


	//sets up and runs the server
	public static void main(String[] args){

		CAMServer cS = new CAMServer();
		cS.initializeServer();
		cS.timer.start();
		
		System.out.println("Running");
		for(;;);
	}//main
}//CatAndMouseServer
